<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            title="设置存储"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="120px">
                <el-form-item label="存储方式" prop="alias">
                    <div>
                        <el-radio model-value>{{ getStorageInfo?.name }}</el-radio>
                        <div class="form-tips">{{ getStorageInfo?.tips }}</div>
                    </div>
                </el-form-item>
                <div v-if="formData.alias !== 'local'">
                    <el-form-item label=" 存储空间名称" prop="bucket">
                        <div class="flex-1">
                            <el-input
                                v-model="formData.bucket"
                                placeholder="请输入存储空间名称(Bucket)"
                                clearable
                            />
                        </div>
                    </el-form-item>
                    <el-form-item label="ACCESS_KEY" prop="accessKey">
                        <el-input
                            v-model="formData.accessKey"
                            placeholder="请输入ACCESS_KEY(AK)"
                            clearable
                        />
                    </el-form-item>
                    <el-form-item label="SECRET_KEY" prop="secretKey">
                        <el-input
                            v-model="formData.secretKey"
                            placeholder="请输入SECRET_KEY(SK)"
                            clearable
                        />
                    </el-form-item>
                    
                    <el-form-item label="endpoint" prop="endpoint">
                        <el-input
                            v-model="formData.endpoint"
                            placeholder="请输入endpoint"
                            clearable
                        />
                    </el-form-item>
                    
                    <el-form-item label="空间域名" prop="domain">
                        <div class="flex-1">
                            <div>
                                <el-input
                                    v-model="formData.domain"
                                    placeholder="请输入空间域名(Domain)"
                                    clearable
                                />
                            </div>
                            <div class="form-tips">
                                请补全http://或https://，例如https://static.cloud.com
                            </div>
                        </div>
                    </el-form-item>
                    <el-form-item
                        v-if="formData.alias == StorageEnum.QCLOUD"
                        label="REGION"
                        prop="region"
                    >
                        <el-input v-model="formData.region" placeholder="请输入region" clearable/>
                    </el-form-item>
                </div>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="formData.status">
                        <el-radio :label="0">关闭</el-radio>
                        <el-radio :label="1">开启</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>


import type {FormInstance} from 'element-plus'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import {StorageEnum, StorageList} from "@/config/enums/StorageEnum";

const emit = defineEmits(['success'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()


/**
 *
 * 数据仓库
 *
 * @author 徐寿春
 * 2023/7/19 09:54
 */
const formData = reactive({
    alias: '',
    bucket: '',
    accessKey: '',
    secretKey: '',
    endpoint: '',
    domain: '',
    region: '', // 腾讯云需要
    status: 0
})


const getStorageInfo = computed(() => {
    return StorageList.find((item) => item.type == formData.alias)
})


/**
 *  处理表单提交
 *
 * @author 徐寿春
 * 2023/7/19 09:55
 */
function handleSubmit() {
    formRef.value?.validate().then(() => {
        // storageSetup(formData)
        
        feedback.msgSuccess('操作成功')
        popupRef.value?.close()
        emit('success')
        
    })
    
    
}


/**
 * 获取当前storage 详情
 *
 * @author 徐寿春
 * 2023/7/19 09:55
 */
async function getDetail() {
    const data = await ({
        alias: formData.alias
    })
    
    for (const key in data) {
        //@ts-ignore
        formData[key] = data[key]
    }
}


/**
 * 调用打开
 *
 * @author 徐寿春
 * 2023/7/19 09:57
 */
const open = (type: string) => {
    formData.alias = type
    popupRef.value?.open()
    getDetail()
}


/**
 * 调用关闭
 *
 * @author 徐寿春
 * 2023/7/19 09:57
 */
function handleClose() {
    formRef.value?.resetFields()
}



defineExpose({
    open
})
</script>
